package com.linzhaowei.headhuntingservice.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.linzhaowei.headhuntingservice.R;
import com.linzhaowei.headhuntingservice.adapter.JobInformationAdapter;
import com.linzhaowei.headhuntingservice.adapter.JobObjectiveAdapter;
import com.linzhaowei.headhuntingservice.bean.JobInformation;
import com.linzhaowei.headhuntingservice.bean.JobObjective;
import com.linzhaowei.headhuntingservice.utils.HttpUtils;
import com.linzhaowei.headhuntingservice.utils.Ip;

import java.util.ArrayList;
import java.util.List;

public class InterviewerJobObjectiveFragment extends Fragment {
    private EditText search_clear;
    private RecyclerView recyclerView;
    private View view;

    private  static final int GET_JOB_OBJECTIVE = 1;

    JobObjectiveAdapter adapter;

    private List<JobObjective> jobObjectiveList=new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_interviewer_job_objective, container, false);
        initViews();
        getJobObjective();
        set_eSearch_TextChanged();
        return view;
    }

    private void initViews(){
        search_clear=view.findViewById(R.id.search_clear_interviewer);
        recyclerView=view.findViewById(R.id.interviewer_job_objective);
    }


    private void getJobObjective(){
        new Thread(() -> {
            HttpUtils httpUtils=new HttpUtils();
            try{
                String result=httpUtils.get(Ip.ip+"/statistics/job_objective");
                JSONArray jsonArray= JSON.parseArray(result);
                for(int i=0;i<jsonArray.size();i++){
                    jobObjectiveList.add(jsonArray
                            .getJSONObject(i)
                            .toJavaObject(JobObjective.class));
                }

                Message message = handler.obtainMessage();
                message.what = GET_JOB_OBJECTIVE;
                handler.sendMessage(message);

            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();

    }


    /**
     * 异步消息处理机制
     */
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GET_JOB_OBJECTIVE:
                    show();
                    break;
                default:
                    break;
            }
        }
    };



    /**
     * 展示
     */
    private void show() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
        recyclerView.setLayoutManager(layoutManager);
        adapter = new JobObjectiveAdapter(jobObjectiveList);
        recyclerView.setAdapter(adapter);
    }

    /**
     * 搜索框文本实时监听
     */
    private void set_eSearch_TextChanged() {
        search_clear.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

                String tempName = search_clear.getText().toString();
                queryData(tempName);
            }
        });
    }


    /**
     * 模糊查询数据 并显示在ListView列表上
     * @param tempName
     */
    private void queryData(String tempName) {

        JobObjectiveAdapter adapter2;
        List<JobObjective> jobObjectiveList2 = new ArrayList<>();

        for (int i = 0; i < jobObjectiveList.size(); i++) {
            if (jobObjectiveList.get(i).getAddress().contains(tempName) ||
                    jobObjectiveList.get(i).getSalary().toString().contains(tempName)||
                    jobObjectiveList.get(i).getName().contains(tempName) ||
                    jobObjectiveList.get(i).getApplicant_name().contains(tempName)) {
                jobObjectiveList2.add(jobObjectiveList.get(i));
            }

        }

        adapter2 = new JobObjectiveAdapter(jobObjectiveList2);
        recyclerView.setAdapter(adapter2);
        adapter2.notifyDataSetChanged();
    }
}
